package com.codename1.samples;

import com.codename1.components.MultiButton;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import com.codename1.ui.*;
import com.codename1.ui.layouts.*;
import com.codename1.ui.plaf.Style;
import java.util.Map;
import com.codename1.io.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename
 * One</a> for the purpose of building native mobile applications using Java.
 */
public class InfiniteContainerSafeAreaTest {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if (err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });
    }

    public void start() {
        if (current != null) {
            current.show();
            return;
        }
        Form hi = new Form("InfiniteContainer", new BorderLayout());

        Style s = UIManager.getInstance().getComponentStyle("MultiLine1");
        FontImage p = FontImage.createMaterial(FontImage.MATERIAL_PORTRAIT, s);
        EncodedImage placeholder = EncodedImage.createFromImage(p.scaled(p.getWidth() * 3, p.getHeight() * 3), false);

        InfiniteContainer ic = new InfiniteContainer() {
            @Override
            public Component[] fetchComponents(int index, int amount) {
                java.util.List<Map<String, Object>> data = fetchPropertyData("Leeds");
                MultiButton[] cmps = new MultiButton[data.size()];
                for (int iter = 0; iter < cmps.length; iter++) {

                    Map<String, Object> currentListing = data.get(iter);
                    if (currentListing == null) {
                        return null;
                    }
                    String thumb_url = (String) currentListing.get("thumb_url");
                    String guid = (String) currentListing.get("guid");
                    String summary = (String) currentListing.get("summary");
                    cmps[iter] = new MultiButton(summary);
                    //cmps[iter].setIcon(URLImage.createToStorage(placeholder, guid, thumb_url));
                }

                return cmps;
            }
        };
        ic.setUIID("Blue");
        ic.setSafeArea(true);
        ic.addComponent(new Label("This is a test"));
        hi.add(BorderLayout.CENTER, ic);
        hi.show();
        
    }

    int pageNumber = 1;
    

    java.util.List<Map<String, Object>> fetchPropertyData(String text) {
        
        CN.invokeAndBlock(()->{
            Util.sleep(5000);
        });
        
        pageNumber++;
        HashMap<String,Object> listing = new HashMap<>();
        listing.put("summary", "This is a test");
        ArrayList<Map<String,Object>> out = new ArrayList<>();
        out.add(listing);
        return out;
    }

    public void stop() {
        current = getCurrentForm();
        if (current instanceof Dialog) {
            ((Dialog) current).dispose();
            current = getCurrentForm();
        }
    }

    public void destroy() {
    }

}
